package com.deluxelx.soul.inn.service.user.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.deluxelx.soul.inn.bean.counselor.Counselor;
import com.deluxelx.soul.inn.bean.counselor.CounselorVO;
import com.deluxelx.soul.inn.bean.user.User;
import com.deluxelx.soul.inn.bean.user.UserInfoDTO;
import com.deluxelx.soul.inn.bean.user.UserInfoVO;
import com.deluxelx.soul.inn.common.util.OssFileUtil;
import com.deluxelx.soul.inn.mapper.user.UserMapper;
import com.deluxelx.soul.inn.service.common.FollowRelationService;
import com.deluxelx.soul.inn.service.counselor.CounselorService;
import com.deluxelx.soul.inn.service.user.UserInfoService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

import java.util.Optional;

import static com.deluxelx.soul.inn.common.constant.CounselorConstant.COUNSELOR_FOLLOW_KEY;
import static com.deluxelx.soul.inn.common.constant.UserRole.*;

/**
 * 用户信息接口实现类
 *
 * @author: zihao.liao
 * @date: 2024/1/24 16:11
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private CounselorService counselorService;

    @Resource
    private FollowRelationService followRelationService;

    @Resource
    private OssFileUtil ossFileUtil;

    @Override
    public Optional<UserInfoVO> fetchUserInfo(String userId) {
        User user = userMapper.selectById(userId);
        if (user == null) {
            return Optional.empty();
        }

        String role = NORMAL_ROLE;
        UserInfoVO userInfo = new UserInfoVO();
        userInfo.setAccount(user.getAccount());
        userInfo.setNickName(user.getNickName());
        userInfo.setGender(user.getGender());
        userInfo.setAvatar(user.getAvatar());
        userInfo.setTelephone(user.getTelephone());
        userInfo.setFollowCount(followRelationService.getMyFollowIds(userId, COUNSELOR_FOLLOW_KEY).size());

        Counselor counselor = counselorService.lambdaQuery()
                .eq(Counselor::getUserId, userId)
                .eq(Counselor::getDeleteFlag, 0)
                .one();
        if (counselor != null) {
            CounselorVO counselorVO = new CounselorVO();
            BeanUtil.copyProperties(counselor, counselorVO);
            userInfo.setCounselorVO(counselorVO);
            role = COUNSELOR_ROLE;
        }
        userInfo.setRole(role);
        return Optional.of(userInfo);
    }

    @Override
    public Boolean modifyUserInfo(UserInfoDTO userInfoDTO) {
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        if (userInfoDTO == null || StrUtil.isBlank(userInfoDTO.getUserId())) {
            return false;
        }
        if (StrUtil.isEmpty(userInfoDTO.getNickName())
                && StrUtil.isEmpty(userInfoDTO.getPassword())
                && userInfoDTO.getSex() == null
                && StrUtil.isEmpty(userInfoDTO.getTelephone())) {
            return false;
        }
        updateWrapper.eq(User::getId, userInfoDTO.getUserId());
        if (StrUtil.isNotEmpty(userInfoDTO.getNickName())) {
            updateWrapper.set(User::getNickName, userInfoDTO.getNickName());
        }
        if (StrUtil.isNotEmpty(userInfoDTO.getPassword())) {
            updateWrapper.set(User::getPassword, userInfoDTO.getPassword());
        }
        if (StrUtil.isNotEmpty(userInfoDTO.getTelephone())) {
            updateWrapper.set(User::getTelephone, userInfoDTO.getTelephone());
        }
        if (null != userInfoDTO.getSex()) {
            updateWrapper.set(User::getGender, userInfoDTO.getSex());
        }
        return userMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public Boolean uploadAvatar(MultipartFile file, String userId) {
        String url = ossFileUtil.upload(file, "userAvatar");
        if (StrUtil.isBlank(url)) {
            return false;
        }
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(User::getId, userId);
        updateWrapper.set(User::getAvatar, url);
        return userMapper.update(null, updateWrapper) > 0;
    }
}
